/*
 *XML Parser class for parsing the state XML */
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import nu.xom.Builder;
import nu.xom.Comment;
import nu.xom.DocType;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Node;
import nu.xom.ParsingException;
import nu.xom.ProcessingInstruction;
import nu.xom.Text;

public class ConceptXMLParser {

	static List dataList = new ArrayList();

	public ConceptXMLParser() {

	}

	public static void parseXMl(File theFileToOpen) {

		Builder builder = new Builder();

		try {
			Document doc = builder.build(theFileToOpen);
			Element root = doc.getRootElement();
			listChildren(root, 0);
		}
		// indicates a well-formedness error
		catch (ParsingException ex) {
			// System.out.println(args[0] + " is not well-formed.");
			System.out.println(ex.getMessage());
		} catch (IOException ex) {
			System.out.println(ex);

		}
	}

	@SuppressWarnings("unchecked")
	public static void listChildren(Node current, int depth)

	{
		String data = "";
		if (current instanceof Element) {
			Element temp = (Element) current;
			data = temp.getQualifiedName();
		} else if (current instanceof ProcessingInstruction) {
			ProcessingInstruction temp = (ProcessingInstruction) current;
			data = temp.getTarget();
		} else if (current instanceof DocType) {
			DocType temp = (DocType) current;
			data = temp.getRootElementName();
		} else if (current instanceof Text || current instanceof Comment) {
			String value = current.getValue();
			value = value.replace('\n', ' ').trim();
			if (value.length() <= 20)
				data = ": " + value;
			else
				data = current.getValue().substring(0, 17);

		}
		// Attributes are never returned by getChild()
		int childCount = 0;

		if (data.equalsIgnoreCase("GUIObject")) {
			childCount = current.getChildCount();
			// System.out.println(current.getChild(0).getValue());
			// System.out.println(current.getChild(1).toString().substring(17,
			// 25));
			dataList.add(current.getChild(0).getValue());
			dataList.add(current.getChild(1).getValue());
			dataList.add(current.getChild(2).getValue());
			dataList.add(current.getChild(3).getValue());
			dataList.add(current.getChild(4).getValue());
			dataList.add(current.getChild(5).getValue());
			dataList.add(current.getChild(6).getValue());
			dataList.add(current.getChild(7).getValue());
			// tableData = new String [][] {{current.getChild(0).getValue()}};

		}

		// System.out.println( data);
		for (int i = 0; i < current.getChildCount(); i++) {
			listChildren(current.getChild(i), depth + 1);
		}
	}

}
